<?php
namespace app\exam\controller;
use app\exam\Model\User;
use app\exam\Model\Questiones;
use app\exam\Model\Descquestion;
use app\exam\Model\Questionimg;
use think\Cache;
use think\Loader;
use think\Validate;
use think\Db;

class Exam extends Base{
    //验证是不是会员
    public function Verification(){
        $data=input();
        $model=model('user');
        $res=$model->getone(['login_flag'=>$data['login_flag']]);
         if($res){
            if(strpos($res['general_vip'], $data['type_id'])!==false||strpos($res['important_vip'], $data['type_id'])!==false){
                return json_encode(['code'=>10000,'msg'=>'是会员']);
            }else{
                return json_encode(['code'=>10001,'msg'=>'不是会员']);
            }
        }else{
             return json_encode(['code'=>10001,'msg'=>'请注册']);
        }

    }

    //随机组卷
    public function  compose(){
        // Cache::clear();die;
        $data=input();
        $question=model('Questiones');
        $desc=model('Descquestion');
        $num=config('TEST_NUM');
        $m=5;
        $n=3;

        // $data['title']=check_str($data['title']);
        $validate = Loader::validate('Exam');
        $id=time();
        $fennum=0;
        $data['single']=intval($data['single']);
        $data['multi']=intval($data['multi']);
        $data['comprehensive']=intval($data['comprehensive']);
        if(!$validate->check($data)){
            return json($validate->getError());
        }
        
        $cache=Cache::get($data['login_flag'].'_'.$data['type_id']);
        if($cache){
            foreach ($cache as $k=>$v){

                if($v['title']==$data['title']){
                    return json_encode(['code'=>10001,'msg'=>'试卷题目已存在']);
                }

            }
        }

        if($data['single']==2&&$data['multi']==1&&$data['comprehensive']==1){
            $res=$question->randSelect($this->sql($data['type_id'],'question_type=0 and comprehensive_id=0',$m*2));
            $num=count($res);
            $resId=[];
           
             foreach ($res as $k1=>&$v) {
                $arr=[];
               $str='';
                    if($v['has_img']==1){
                       $ret=Db::table('questionimg')->where(['question_id'=>$v['question_id']])->select();
                       foreach ($ret as $k => $v1) {
                          $str.=$v1['img_url'].',';
                       }
                       
                      $v['img']=$str;

                    }
                    $resId[$k1]=$v['question_id'];
                  
                    $arr[]=$res;
  
            }
            $res=$arr;
            $zonghe=[];
            // print_r($arr);
            // print_r($resId);die;

        }else if($data['single']==2&&$data['multi']==2&&$data['comprehensive']==1){
            $rek=$question->randSelect($this->sql($data['type_id'],'question_type=0  and comprehensive_id=0',$m));
            $res=$question->randSelect($this->sql($data['type_id'],'question_type=3  and comprehensive_id=0',$m));

            $res= array_merge($rek,$res);
            $num=count($res);
            $resId=[];
             foreach ($res as $k1=>&$v) {
                $arr=[];
               $str='';
                    if($v['has_img']==1){
                       $ret=Db::table('questionimg')->where(['question_id'=>$v['question_id']])->select();
                       foreach ($ret as $k => $v1) {
                          $str.=$v1['img_url'].',';
                       }
                       
                      $v['img']=$str;
                    }
                    $resId[$k1]=$v['question_id'];
                 
                      $arr[]=$res;
            }
            $res=$arr;
             $zonghe=[];
//             echo $num;
//           print_r($res);
//           print_r($resId);die;
        }else if($data['single']==2&&$data['multi']==2&&$data['comprehensive']==2){
            $res=$question->randSelect($this->sql($data['type_id'],' question_type=0 and comprehensive_id=0',$m));
            $rek=$question->randSelect($this->sql($data['type_id'],'question_type=3  and comprehensive_id=0',$n));
            $res= array_merge($rek,$res);
            $num=count($res);
            $resId=[];
              foreach ($res as $k1=>&$v5) {
                $arr3=[];
               $str1='';
                    if($v5['has_img']==1){
                       $repty=Db::table('questionimg')->where(['question_id'=>$v5['question_id']])->select();
                       foreach ($repty as $k => $v6) {
                          $str1.=$v6['img_url'].',';
                       }
                       
                      $v5['img']=$str1;
                    }
                    
                     $resId[$k1]=$v5['question_id'];
          
                     $arr3[]=$res;
         
            }
//            $number=floor(rand(1,5));
            $number=$m-$n;
            $resupt=$desc->selectWhole("SELECT * FROM descquestion WHERE type_num=".$data['type_id']." ORDER BY rand() limit $number");
            $str='';
            foreach ($resupt as $v){
                $str.=$v['c_id'].',';
            }
            $str=rtrim($str,',');
            $str=explode(',',$str);
            $fennum=count($resupt);
//            $fennum= Db::table('questiones')->where(['comprehensive_id'=>['in',$str]])->count();

            $arr=[];
            $resarray=[];
            foreach ($resupt as $k=>&$v){

              $q_id=[];
               $ret= Db::table('questiones')->where([ 'comprehensive_id'=>$v['c_id']])->select();

                 foreach ($ret as $k1=>&$v2) {
                  
               $str3='';
                    if($v2['has_img']==1){
                       $retp=Db::table('questionimg')->where(['question_id'=>$v2['question_id']])->select();
                       foreach ($retp as $k3 => $v1) {
                          $str3.=$v1['img_url'].',';
                       }
                       
                      $v2['img']=$str3;
                    }
                    $q_id[]=$v2['question_id'];
                    $resarray[$v['c_id']]=$q_id;
                     $v2['desc']=$v['c_decs'];
                       $v2['c_id']=$v['c_id'];
            }

                $arr[$v['c_id']]=$ret;

            }
            $res=array_merge($arr3,$arr);
            $zonghe=$resarray;

//             print_r($resarray);
//             print_r($resId);
//           print_r($res);die;

        }else if($data['single']==1&&$data['multi']==2&&$data['comprehensive']==2){
            $res=$question->randSelect($this->sql($data['type_id'],'question_type=3 and comprehensive_id=0',$m+$n));
            $num=count($res);
            $resId=[];
             foreach ($res as $k1=>&$v5) {
                $arr3=[];
               $str1='';
                    if($v5['has_img']==1){
                       $repty=Db::table('questionimg')->where(['question_id'=>$v5['question_id']])->select();
                       foreach ($repty as $k => $v6) {
                          $str1.=$v6['img_url'].',';
                       }
                       
                      $v5['img']=$str1;
                    }
                      $resId[$k1]=$v5['question_id'];
                    // $resId[$k1]['status']=0;
                    // $resId[$k1]['is_true']=0;
                      $arr3[]=$res;
            }
            $number=$m-$n;
//            $number=floor(rand(1,5));
            $resupt=$desc->selectWhole("SELECT * FROM descquestion WHERE type_num=".$data['type_id']." ORDER BY rand() limit $number");
            $str='';
            foreach ($resupt as $v){
                $str.=$v['c_id'].',';
            }
            $str=rtrim($str,',');
            $str=explode(',',$str);
            $fennum=count($resupt);
//            $fennum= Db::table('questiones')->where(['comprehensive_id'=>['in',$str]])->count();
//            print_r($str);
//            print_r($fennum);die;
            $arr=[];
            $resarray=[];
            foreach ($resupt as &$v){
               $q_id=[];
                $ret= Db::table('questiones')->where(['comprehensive_id'=>$v['c_id']])->select();
                 foreach ($ret as &$v2) {
               $str3='';
                    if($v2['has_img']==1){
                       $retp=Db::table('questionimg')->where(['question_id'=>$v2['question_id']])->select();
                       foreach ($retp as $k => $v1) {
                          $str3.=$v1['img_url'].',';
                       }

                      $v2['img']=$str3;
                    }
                     $v2['desc']=$v['c_decs'];
                       $v2['c_id']=$v['c_id'];
                       $q_id[]=$v2['question_id'];
                    $resarray[$v['c_id']]=$q_id;
                    // $resarray['status']=0;
                    // $resarray['is_true']=0;
                   // array_push($resId, $resarray);
                   
            }

                $arr[$v['c_id']]=$ret;
                // $arr[$v['c_id']]['desc']=$v['c_decs'];
                // $arr[$v['c_id']]['c_id']=$v['c_id'];

            }
//            $num=$m*2-$fennum;
            $res=array_merge($arr3,$arr);
            $zonghe=$resarray;
//            echo $num;
//            echo $fennum;
//             print_r($resId);
//             print_r($resarray);
//             print_r($res);die;

        }else if($data['single']==1&&$data['multi']==1&&$data['comprehensive']==2){
            //$res=$question->randSelect($this->sql($data['type_id'],'question_type=2',5));
//              $number=floor(rand(1,5));
            $number=$m*2;
              $resId=[];

            $resupt=$desc->selectWhole("SELECT * FROM descquestion WHERE type_num=".$data['type_id']." ORDER BY rand() limit $number");
            $str='';
            foreach ($resupt as $v){
                $str.=$v['c_id'].',';
            }
            $str=rtrim($str,',');
            $str=explode(',',$str);
            $fennum=count($resupt);
//            $fennum= Db::table('questiones')->where(['comprehensive_id'=>['in',$str]])->count();

             $arr=[];
              $resarray=[];
            foreach ($resupt as $key=>&$v){
               $q_id=[];
                $ret= Db::table('questiones')->where(['comprehensive_id'=>$v['c_id']])->select();

                 foreach ($ret as &$v2) {
                     
               $str3='';
                    if($v2['has_img']==1){
                       $retp=Db::table('questionimg')->where(['question_id'=>$v2['question_id']])->select();
                       foreach ($retp as $k => $v1) {
                          $str3.=$v1['img_url'].',';
                       }
                       
                      $v2['img']=$str3;
                    }
                    $q_id[]=$v2['question_id'];
                     $resarray[$v['c_id']]=$q_id;
                    // $resarray['status']=0;
                    // $resarray['is_true']=0;
                    $v2['desc']=$v['c_decs'];
                       $v2['c_id']=$v['c_id'];
                   // array_push($resId, $resarray);

            }

                $arr[$key]=$ret;
                // $arr[$key]['desc']=$v['c_decs'];
                // $arr[$key]['c_id']=$v['c_id'];

            }
            $num=0;
            $res=$arr;
            $zonghe=$resarray;
//            echo $fennum;
//               print_r($zonghe);
//             // print_r($ret);
//             print_r($resId);
//             print_r($res);die;

        }else if($data['single']==1&&$data['multi']==2&&$data['comprehensive']==1){
            $res=$question->randSelect($this->sql($data['type_id'],'question_type=3',$m*2));
            $num=count($res);
             $resId=[];
             foreach ($res as $k1=>&$v) {
                $arr=[];
               $str='';
                    if($v['has_img']==1){
                       $ret=Db::table('questionimg')->where(['question_id'=>$v['question_id']])->select();
                       foreach ($ret as $k => $v1) {
                          $str.=$v1['img_url'].',';
                       }
                       
                      $v['img']=$str;
                    }
                    $resId[$k1]=$v['question_id'];
                    // $resId[$k1]['status']=0;
                    // $resId[$k1]['is_true']=0;
                    $arr[]=$res;

            }
            $res=$arr;
            $zonghe=[];
            //  print_r($resId);
            // print_r($res);die;

        }else if($data['single']==2&&$data['multi']==1&&$data['comprehensive']==2){
            $res=$question->randSelect($this->sql($data['type_id'],'question_type=0 and comprehensive_id=0',$m));
            $num=count($res);
            $resId=[];
             foreach ($res as $k1=>&$v5) {
                $arr3=[];
               $str1='';
                    if($v5['has_img']==1){
                       $repty=Db::table('questionimg')->where(['question_id'=>$v5['question_id']])->select();
                       foreach ($repty as $k => $v6) {
                          $str1.=$v6['img_url'].',';
                       }
                       
                      $v5['img']=$str1;
                    }
                   $resId[$k1]=$v5['question_id'];
                    // $resId[$k1]['status']=0;
                    // $resId[$k1]['is_true']=0;
                    $arr3[]=$res;
            }
            $number=$m;
            $resupt=$desc->selectWhole("SELECT * FROM descquestion WHERE type_num=".$data['type_id']." ORDER BY rand() limit $number");
            $str='';
            foreach ($resupt as $v){
                $str.=$v['c_id'].',';
            }
            $str=rtrim($str,',');
            $str=explode(',',$str);
            $fennum=count($resupt);
//            $fennum= Db::table('questiones')->where(['comprehensive_id'=>['in',$str]])->count();

            $arr=[];
             $resarray=[];
            foreach ($resupt as &$v){
              $q_id=[];
                $ret= Db::table('questiones')->where(['comprehensive_id'=>$v['c_id']])->select();
                 foreach ($ret as &$v2) {
                    
               $str3='';
                    if($v2['has_img']==1){
                       $retp=Db::table('questionimg')->where(['question_id'=>$v2['question_id']])->select();
                       foreach ($retp as $k => $v1) {
                          $str3.=$v1['img_url'].',';
                       }
                       
                      $v2['img']=$str3;
                    }
                    $q_id[]=$v2['question_id'];
                     $resarray[$v['c_id']]=$q_id;
                    // $resarray['status']=0;
                    // $resarray['is_true']=0;
                     $v2['desc']=$v['c_decs'];
                    $v2['c_id']=$v['c_id'];
                   // array_push($resId, $resarray);
            }

                $arr[$v['c_id']]=$ret;
                // $arr[$v['c_id']]['desc']=$v['c_decs'];
                // $arr[$v['c_id']]['c_id']=$v['c_id'];

            }
//            $num=$num-5;
            $res=array_merge($arr3,$arr);
            $zonghe=$resarray;
//            echo $num;echo $fennum;
//             print_r($res);
//             print_r($resarray);
//             print_r($resId);die;

        }else if($data['single']==1&&$data['multi']==1&&$data['comprehensive']==1){
            $res='';
           
        }
            
            if($res){

                $arr1[]=[
                    'id'=>$id,
                    'reg_time'=>time(),
                    'title'=>$data['title'],
                    'fen'=>$fennum+$num
                ];
        //    print_r($res); 
        //    print_r($fennum+$num);
        // print_r($resId);die;
                Cache::set($data['login_flag'].'_'.$id.'_'.$data['type_id'],$res);
                 Cache::set($data['login_flag'].'_'.$id.'danxuanduoxuan_datika'.'_'.$data['type_id'],$resId);
                 Cache::set($data['login_flag'].'_'.$id.'zonghe_datika'.'_'.$data['type_id'],$zonghe);
                $b=Cache::get($data['login_flag'].'_'.$data['type_id']);

                if($b){
                    $data_push=[
                        'id'=>$id,
                        'reg_time'=>time(),
                        'title'=>$data['title'],
                        'fen'=>$fennum+$num
                    ];
                    array_push($b,$data_push);
                    Cache::set($data['login_flag'].'_'.$data['type_id'],$b);
                }else{
                    Cache::set($data['login_flag'].'_'.$data['type_id'],$arr1);
                }


                  return json_encode(['code'=>10000,'msg'=>'组卷成功']);
            }else{
                 return json_encode(['code'=>10001,'msg'=>'组卷失败试题为空或者用户没有选择题型']);
            }
            //return self::three($res,'组卷成功','组卷失败或者用户没有选择题型');



    }

    public function  entryList(){
        $data=input();
        $list=Cache::get($data['login_flag'].'_'.$data['type_id']);
        //print_r($data);die;
       return self::three($list,'查询成功','暂无试卷',$list);

    }

    public function answer(){
        $data=input();
        if($data){
            $res=Cache::get($data['login_flag'].'_'.$data['id'].'_'.$data['type_id']);
             // print_r($res);
            return self::three($res,'查询成功','查询失败',$res);
        }


    }

    public function delete(){
        $data=input();
        if($data){
            $res=Cache::get($data['login_flag'].'_'.$data['type_id']);
            foreach ($res as $k=>$v) {
                
                if($v['id']==$data['id']){
                    $v=[];
                    unset($res[$k]);
                }
            }
            $res=Cache::set($data['login_flag'].'_'.$data['type_id'],$res);
            //print_r($res);die;

            $b=Cache::rm($data['login_flag'].'_'.$data['id'].'_'.$data['type_id']);
             return self::three($res,'操作成功','操作失败');
            //print_r($b);
        }
    }


    public function  sql($data,$where='',$num){

        return "SELECT * FROM questiones WHERE type_num=".$data." AND ".$where." ORDER BY rand() LIMIT ".$num;
    }
    public function  type_sql($where=''){

        return "SELECT * FROM questiones WHERE ".$where;
    }
}